import matplotlib.pyplot as plt
import numpy as np

R = 20      # 滑动变阻器阻值
Rx = 10     # 待测电阻阻值
E = 4.5     # 电源电动势
r = 0.1     # 电源内阻
Rv = 10000  # 电压表内阻
Ra = 4      # 电源表内阻
R_slide = np.linspace(0, R, 400)  # 滑动变阻器并联部分阻值 或 接入电路的阻值 

def A_in_xian_liu():
    R1 = Rx + Ra
    R2 = Rv * R1 / (R1 + Rv)
    R3 = R2 + r + R_slide
    Uv = R2 * E / R3
    URx = Rx * Uv / R1
    Ia = URx / Rx
    return (Uv, Ia, URx, Ia, Ia)

def A_out_xian_liu():
    R1 = Rx * Rv / (Rx + Rv)
    R2 = R1 + Ra
    R3 = R2 + R_slide + r
    Uv = R1 * E / R3
    Ia = Uv / R1
    IRx = Uv / Rx
    return(Uv, Ia, Uv, IRx, Ia)

def A_in_fen_ya():
    R1 = Rx + Ra
    R2 = R1 * Rv / (R1 + Rv)
    R3 = (R - R_slide) * R2 / (R - R_slide + R2)
    R4 = R3 + R - R_slide + r
    Uv = R3 * E / R4
    URx = Rx * Uv / R1
    Ia = URx / Rx
    return (Uv, Ia, URx, Ia, E / R4)

# 外接
def A_out_fen_ya():
    R1 = Rx * Rv / (Rx + Rv)
    R2 = R1 + Ra
    R3 = (R - R_slide) * R2 / (R - R_slide + R2)
    R4 = R3 + R_slide + r
    U1 = R3 * E / R4
    Uv = R1 * U1 / R2
    Ia = Uv / R1
    IRx = Uv / Rx
    return (Uv, Ia, Uv, IRx, E / R4)

# 设置图片清晰度
plt.rcParams['figure.dpi'] = 72
plt.rcParams['font.family'] = 'SimHei'

# 绘制函数图像

plt.plot(R_slide, A_out_xian_liu()[0] * A_out_xian_liu()[1], label='内接-限流-功率-测量值 Rx=10,R=' + str(R))
plt.plot(R_slide, A_out_fen_ya()[0] * A_out_fen_ya()[1], label='外接-分压-功率-测量值 Rx=10,R=' + str(R))
plt.plot(R_slide, E * A_out_xian_liu()[4], label='内接-限流-总功率-测量值 Rx=10,R=' + str(R))
plt.plot(R_slide, E * A_out_fen_ya()[4], label='外接-分压-总功率-测量值 Rx=10,R=' + str(R))
# plt.plot(R_slide, A_out_xian_liu()[1], label='内接-限流-电流-测量值 Rx=10,R=' + str(R))
# plt.plot(R_slide, A_out_fen_ya()[0], label='外接-分压-电压-测量值 Rx=10,R=' + str(R))
# plt.plot(R_slide, A_out_fen_ya()[1], label='外接-分压-电流-测量值 Rx=10,R=' + str(R))

# 添加标题和标签
plt.title('限流式与分压式接法')
plt.xlabel('滑动变阻器阻值')
plt.ylabel('电压')

# 显示图例
plt.legend()

# 显示网格线
plt.grid(True)

# 显示图像
plt.show()